#!/bin/bash

echo "EbbFlow Protocol Performance Test"
echo "================================="

# Configuration
NODES=6
FAULTY=1
CLIENTS=3
TRANSACTIONS=5000
BATCH_SIZE=1

# Create logs directory
mkdir -p logs

# Function to measure component delays
measure_delays() {
    local log_file=$1
    local component=$2
    
    echo "Measuring $component delays..."
    
    # Extract timing information from logs
    if [ -f "$log_file" ]; then
        # RBC delay
        rbc_delay=$(grep "RBC.*delay" "$log_file" | awk '{sum+=$NF; count++} END {if(count>0) print sum/count; else print "N/A"}')
        
        # ABA delay  
        aba_delay=$(grep "ABA.*delay" "$log_file" | awk '{sum+=$NF; count++} END {if(count>0) print sum/count; else print "N/A"}')
        
        # LT Code delay
        lt_delay=$(grep "LT.*delay" "$log_file" | awk '{sum+=$NF; count++} END {if(count>0) print sum/count; else print "N/A"}')
        
        # Proof Chain delay
        proof_delay=$(grep "Proof.*delay" "$log_file" | awk '{sum+=$NF; count++} END {if(count>0) print sum/count; else print "N/A"}')
        
        # Overall consensus delay
        consensus_delay=$(grep "Consensus.*delay" "$log_file" | awk '{sum+=$NF; count++} END {if(count>0) print sum/count; else print "N/A"}')
        
        echo "  RBC Delay: ${rbc_delay}ms"
        echo "  ABA Delay: ${aba_delay}ms" 
        echo "  LT Code Delay: ${lt_delay}ms"
        echo "  Proof Chain Delay: ${proof_delay}ms"
        echo "  Consensus Delay: ${consensus_delay}ms"
    else
        echo "  Log file not found: $log_file"
    fi
}

# Function to calculate TPS
calculate_tps() {
    local log_file=$1
    local component=$2
    
    if [ -f "$log_file" ]; then
        # Extract TPS from logs
        tps=$(grep "TPS\|throughput" "$log_file" | tail -1 | awk '{print $NF}')
        if [ -n "$tps" ]; then
            echo "  $component TPS: $tps"
        else
            echo "  $component TPS: N/A"
        fi
    else
        echo "  $component TPS: N/A"
    fi
}

# Start servers
echo "Starting EbbFlow servers..."
for i in $(seq 0 $((NODES-1))); do
    echo "Starting server $i..."
    ./bin/ebbflow_server -id $i -n $NODES -f $FAULTY > logs/server_$i.log 2>&1 &
    SERVER_PIDS[$i]=$!
    sleep 2
done

echo "Waiting for servers to initialize..."
sleep 10

# Start clients
echo "Starting EbbFlow clients..."
for i in $(seq 1 $CLIENTS); do
    echo "Starting client $((100+i))..."
    ./bin/ebbflow_client -id $((100+i)) -batch $BATCH_SIZE -count $TRANSACTIONS > logs/client_$((100+i)).log 2>&1 &
    CLIENT_PIDS[$i]=$!
    sleep 1
done

echo "Running performance test for 60 seconds..."
sleep 60

# Stop clients
echo "Stopping clients..."
for pid in "${CLIENT_PIDS[@]}"; do
    kill $pid 2>/dev/null
done

# Stop servers
echo "Stopping servers..."
for pid in "${SERVER_PIDS[@]}"; do
    kill $pid 2>/dev/null
done

echo ""
echo "Performance Test Results"
echo "========================"

# Calculate overall TPS
echo "Overall Performance:"
total_tps=0
client_count=0

for i in $(seq 1 $CLIENTS); do
    client_log="logs/client_$((100+i)).log"
    if [ -f "$client_log" ]; then
        tps=$(grep "TPS\|throughput" "$client_log" | tail -1 | awk '{print $NF}')
        if [ -n "$tps" ]; then
            echo "  Client $((100+i)) TPS: $tps"
            total_tps=$((total_tps + tps))
            client_count=$((client_count + 1))
        fi
    fi
done

if [ $client_count -gt 0 ]; then
    avg_tps=$((total_tps / client_count))
    echo "  Average TPS: $avg_tps"
    echo "  Total TPS: $total_tps"
fi

echo ""
echo "Component Delays:"

# Measure delays for each server
for i in $(seq 0 $((NODES-1))); do
    echo "Server $i:"
    measure_delays "logs/server_$i.log" "Server-$i"
done

# Measure delays for each client
for i in $(seq 1 $CLIENTS); do
    echo "Client $((100+i)):"
    measure_delays "logs/client_$((100+i)).log" "Client-$((100+i))"
done

echo ""
echo "Proof Chain Analysis:"
echo "====================="

# Analyze proof chain performance
for i in $(seq 0 $((NODES-1))); do
    server_log="logs/server_$i.log"
    if [ -f "$server_log" ]; then
        proof_count=$(grep -c "Proof.*generated\|Proof.*verified" "$server_log" 2>/dev/null || echo "0")
        echo "  Server $i: $proof_count proof operations"
    fi
done

echo ""
echo "Test completed. Check logs/ directory for detailed logs."






